package org.linlinjava.litemall.admin.service;

import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.LitemallPurchaseService;
import org.linlinjava.litemall.db.service.LitemallShopService;
import org.linlinjava.litemall.db.util.SequenceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class AdminPurchaseService {

    @Autowired
    private LitemallPurchaseService purchaseService;

    @Autowired
    private LitemallShopService shopService;

    public LitemallFormPurchase selectByPrimaryKey(Integer id){
        return purchaseService.selectByPrimaryKey(id);
    }

    public List<LitemallFormPurchase> list(String status, String sort,String startTime,String endTime,String needHerb, String order, Integer page, Integer limit, boolean selectAll) {
        LitemallFormPurchaseExample example = new LitemallFormPurchaseExample();
        LitemallFormPurchaseExample.Criteria criteria = example.createCriteria();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Subject currentUser = SecurityUtils.getSubject();
        LitemallAdmin admin = (LitemallAdmin) currentUser.getPrincipal();

        if (!StringUtils.isEmpty(status)){
            criteria.andStatusEqualTo(Byte.valueOf(status));
        }

        if (!StringUtils.isEmpty(admin.getShopId())){
            criteria.andShopIdEqualTo(Integer.valueOf(admin.getShopId()));
        }

        if (!StringUtils.isEmpty(needHerb)){
            criteria.andNeedHerbLike("%"+needHerb+"%");
        }

        if (!StringUtils.isEmpty(startTime)){
            criteria.andAddTimeGreaterThan(LocalDateTime.parse(startTime,df));
        }

        if (!StringUtils.isEmpty(endTime)){
            criteria.andAddTimeLessThan(LocalDateTime.parse(endTime,df));
        }


        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        if(!selectAll){
            PageHelper.startPage(page, limit);
        }

        return purchaseService.selectByExample(example);
    }
//
    public LitemallFormPurchase add(LitemallFormPurchase purchaseExample) {
        Subject currentUser = SecurityUtils.getSubject();
        LitemallAdmin admin = (LitemallAdmin) currentUser.getPrincipal();
        purchaseExample.setUserId(admin.getId());
        purchaseExample.setId((int)System.currentTimeMillis());
        purchaseExample.setCode(SequenceUtils.getSupplySequenceCode());
        LitemallShop shop = shopService.findById(admin.getShopId());
        purchaseExample.setShopId(shop.getId());
        purchaseExample.setShopName(shop.getCompanyName());
        purchaseExample.setUserType(shop.getUserType());
        if(purchaseExample.getStatus().equals(Byte.parseByte("1"))){
            purchaseExample.setPublishDt(LocalDateTime.now());
        }
        purchaseExample.setAddTime(LocalDateTime.now());
        purchaseExample.setUpdateTime(LocalDateTime.now());
        purchaseService.insertSelective(purchaseExample);
        return purchaseExample;
    }
    public void updateById(LitemallFormPurchase purchaseExample) {
        purchaseExample.setUpdateTime(LocalDateTime.now());
        if(purchaseExample.getStatus().equals(Byte.parseByte("1"))){
            purchaseExample.setPublishDt(LocalDateTime.now());
        }
        purchaseService.updateByPrimaryKeySelective(purchaseExample);
    }
//
//    public void delete(LitemallHerb herbExample) {
//        herbExample.setUpdateTime(LocalDateTime.now());
//        herbExample.setDeleted(true);
//        herbService.updateByPrimaryKeySelective(herbExample);
//    }
//
//    public boolean checkHerbNameExist(String id,String name) {
//        LitemallHerbExample example = new LitemallHerbExample();
//        LitemallHerbExample.Criteria criteria = example.createCriteria();
//
//        if (!StringUtils.isEmpty(id)){
//            criteria.andIdNotEqualTo(Integer.valueOf(id));
//        }
//        criteria.andHerbNameEqualTo(name);
//        criteria.andDeletedEqualTo(false);
//
//        return herbService.countByExample(example) != 0;
//    }
//    public boolean checkCodeExist(String id,String code) {
//        LitemallHerbExample example = new LitemallHerbExample();
//        LitemallHerbExample.Criteria criteria = example.createCriteria();
//
//        if (!StringUtils.isEmpty(id)){
//            criteria.andIdNotEqualTo(Integer.valueOf(id));
//        }
//        criteria.andCodeEqualTo(code);
//        criteria.andDeletedEqualTo(false);
//
//        return herbService.countByExample(example) != 0;
//    }
//
//    public int batchInsert(List<LitemallHerb> list){
//        return herbService.batchInsert(list);
//    }



}
